﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using OpenPhotoAlbum.Controls;
using OpenPhotoAlbum.Controls.PhotoProcessors;

namespace OpenPhotoAlbum.My
{
	public partial class Photo : AlbumPage
	{
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

			this.Title = StringUtility.Format(Resources.Strings.Title_My_Photo, UrlContext.AlbumTitle, CurrentPhoto.PhotoTitle);

			tabPanels.TabPanelItems.Add(new TabPanelItem()
			{
				Name = Resources.Strings.VE,
				PanelClientId = pnlVisualEffects.ClientID,
				Visible = false
			});
			tabPanels.TabPanelItems.Add(new TabPanelItem()
			{
				Name = Resources.Strings.SharingAddress,
				PanelClientId = pnlSharingAddress.ClientID,
				Visible = false
			});
			
			hlVisit.Text = Resources.Strings.VisitThisAddress;
			hlVisit.Target = "_blank";

			linkBtnDelete.Text = Resources.Strings.Delete;
			linkBtnDelete.Click += new EventHandler(linkBtnDelete_Click);
			linkBtnDelete.OnClientClick = StringUtility.GetConfirmJs(Resources.Strings.My_Confirm_DeletePhoto);

			ttTitle.Text = Resources.Strings.Tooltip_ClickToEdit;
			lblTitle.TooltipBehaviorId = ttTitle.ClientID;

			ttDescription.Text = Resources.Strings.Tooltip_ClickToEdit;
			lblDescription.TooltipBehaviorId = ttDescription.ClientID;
		}

		protected override void AppendSiteMapNodes(AlbumSiteMapProvider provider)
		{
			base.AppendSiteMapNodes(provider);

			provider.AppendSiteMapNode(Resources.Strings.SC_Home, Links.Default);
			provider.AppendSiteMapNode(Resources.Strings.My_Home, Links.MyHome);
			provider.AppendSiteMapNode(UrlContext.AlbumTitle, UrlContext.BuildMyAlbumUrl(UrlContext.HomePageNumber, UrlContext.AlbumId, UrlContext.AlbumTitle, UrlContext.AlbumPageNumber));
			provider.AppendSiteMapNode(CurrentPhoto.PhotoTitle, RawUrl);
		}

		protected void linkBtnDelete_Click(object sender, EventArgs e)
		{
			Guid albumId = BusinessLogics.PhotoManager.Instance.GetPhoto(UrlContext.PhotoId).AlbumId;

			BusinessLogics.PhotoManager.Instance.DeletePhoto(UrlContext.PhotoId);

			Response.Redirect(UrlContext.BuildMyAlbumUrl(UrlContext.HomePageNumber, UrlContext.AlbumId, UrlContext.AlbumTitle, UrlContext.AlbumPageNumber));
		}

		private DbAccess.Photo _photo = null;
		protected DbAccess.Photo CurrentPhoto
		{
			get
			{
				if (_photo == null)
				{
					_photo = BusinessLogics.PhotoManager.Instance.GetPhoto(UrlContext.PhotoId);
				}
				return _photo;
			}
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			imgPhoto.ImageUrl = UrlContext.BuildPhotoPipeUrl(UrlContext.PhotoId, BusinessLogics.PhotoTypes.FullSize);

			DbAccess.Photo photo = this.CurrentPhoto;

			lblTitle.Text = photo.PhotoTitle;
			lblTitle.ContextData1 = UrlContext.PhotoId.ToString();
			if (string.IsNullOrEmpty(photo.PhotoDescription)) { lblDescription.Text = string.Empty; }
			else { lblDescription.Text = photo.PhotoDescription; }
			lblDescription.ContextData1 = UrlContext.PhotoId.ToString();
			lblCreationDate.Text = string.Format(
				CultureInfo.CurrentCulture,
				Resources.Strings.My_UploadDate,
				photo.PhotoCreationDate.ToLongDateString());

			string sharingAddress = UrlContext.BuildSharedPhotoUrl(photo.PhotoId);
			ltrSharingAddress.Text = HttpUtility.HtmlEncode(Links.ToFullPath(sharingAddress));
			hlVisit.NavigateUrl = sharingAddress;
		}

		public string EdgeDetectionUrl
		{
			get
			{
				return UrlContext.BuildPhotoPipeVisualEffectUrl(
					UrlContext.PhotoId, BusinessLogics.PhotoTypes.FullSize, VisualEffects.EdgeDetection);
			}
		}

		public string NegativeImageUrl
		{
			get
			{
				return UrlContext.BuildPhotoPipeVisualEffectUrl(
					UrlContext.PhotoId, BusinessLogics.PhotoTypes.FullSize, VisualEffects.NegativeImage);
			}
		}

		public string EmbossUrl
		{
			get
			{
				return UrlContext.BuildPhotoPipeVisualEffectUrl(
					UrlContext.PhotoId, BusinessLogics.PhotoTypes.FullSize, VisualEffects.Emboss);
			}
		}

		public string DefaultUrl
		{
			get
			{
				return UrlContext.BuildPhotoPipeUrl(
					UrlContext.PhotoId, BusinessLogics.PhotoTypes.FullSize);
			}
		}

		public string UrlFormat
		{
			get
			{
				return UrlContext.BuildPhotoPipeVisualEffectUrl(UrlContext.PhotoId, BusinessLogics.PhotoTypes.FullSize, "{0}");
			}
		}

		public string AlbumUrl
		{
			get
			{
				DbAccess.Album album = this.CurrentPhoto.Album;
				return UrlContext.BuildMyAlbumUrl(0, album.AlbumId, album.AlbumTitle, 0);
			}
		}
	}
}
